<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>角色列表</title>
    <#include "/layout/css.html"/>
    <link rel="stylesheet" href="${basePath}/ztree/css/zTreeStyle/zTreeStyle.css">
</head>
<body class="body">

<!-- 工具集 -->
<div class="my-btn-box">
</div>
<span class="fl">
	<button class="layui-btn mgl-20" id="btn-add">提交</button>
</span>
<hr>
<!-- 表格 -->
<div class="content_wrap">
	<div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
</div>
<#include "/layout/js.html"/>
<script type="text/javascript" src="${basePath}/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${basePath}/ztree/js/jquery.ztree.excheck.js"></script>
<SCRIPT type="text/javascript">
		var setting = {
				check: {
					enable: true,
					autoCheckTrigger: true
				},
			data: {
				simpleData: {
					enable: true
				}
			}
		};
		var checkList = new Array();
		function verifyCheck(id){
			if(checkList==null){
				return false;
			}
			for (var i=0;i<checkList.length;i++)
			{	
				if(checkList[i] == id){
					return true;
				}
			}
			return false;
		}
		function init(){
			authManagerHttp({url:"/menu/roleMenulistData",data:{"systemCode":"${sysMenu.systemCode!}","sysRoleId":"${roleId!}"}},{all:function(data){
        		if(data.isOk){
        			console.log("data.data.records");
        			console.log(data.data.records);
        			checkList =new Array();
        			for (var i=0;i<data.data.records.length;i++)
        			{	
        				console.log("-- "+data.data.records[i].sysMenuId);
        				var zNode = new Object();
        				checkList[i]=data.data.records[i].sysMenuId;
        			}
        			console.log("checkList");
        			console.log(checkList);
        			initZtree();
            	}else{
            		layer.msg(data.message,{time: 3000});
            	}
        	}})
		}
		function initZtree() {
			authManagerHttp({url:"/menu/listData",data:{"sysMenuId":"${systemId!}"}},{all:function(data){
        		if(data.isOk){
        			var zNodes =new Array();
        			for (var i=0;i<data.data.records.length;i++)
        			{	
        				var zNode = new Object();
        				zNode.id=data.data.records[i].sysMenuId;
        				zNode.pId=data.data.records[i].parentId;
        				zNode.name=data.data.records[i].name;
        				zNode.open=true;
        				zNode.checked=verifyCheck(data.data.records[i].sysMenuId);
        				zNodes[i]=zNode;
        			}
        			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
            	}else{
            		layer.msg(data.message,{time: 3000});
            	}
        	}})
		}
			
		function getCheckId(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getCheckedNodes(true);
			console.log(nodes);
			if (nodes.length == 0) {
				return "";
			}
			var zNodes =new Array();
			for (var i=0;i<nodes.length;i++)
			{	
				zNodes[i]=nodes[i].id;
			}
			return zNodes.join();
		}
		
		function addData(sysMenuIds){
        	authManagerHttp({url:"/menu/saveGiveData",data:{"systemCode":"${sysMenu.systemCode!}","sysRoleId":"${roleId!}","sysMenuIds":sysMenuIds}},{success:function(data){
        		init()
        	}})
        }
		
		$('#btn-add').on('click', function(){
			var checkId = getCheckId();
			addData(checkId);
		});
		
		$(document).ready(function(){
			init();			
		});
	</SCRIPT>
</body>
</html>